package com.kt.uokos.yimo.controller;


import com.kt.uokos.yimo.dao.Category;
import com.kt.uokos.yimo.service.CategoryService;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.vo.CategoryVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("l1")
    public Object l1(){
        List<Category> categoryL1List = categoryService.queryL1();
        ArrayList<Map<String,Object>> data = new ArrayList<>(categoryL1List.size());
        for (Category category : categoryL1List){
            HashMap<String, Object> map = new HashMap<>();
            map.put("value",category.getId());
            map.put("label",category.getName());
            data.add(map);
        }
        return ResponseUtil.okList(data);
    }


    @GetMapping("/list")
    public Object list(){
        List<CategoryVo> categoryVoList = new ArrayList<>();
        //查询最大类目
        List<Category> categoryList = categoryService.queryByPid(0);
        for (Category category :categoryList){
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(category.getId());
            categoryVo.setDesc(category.getDesc());
            categoryVo.setIconUrl(category.getIconUrl());
            categoryVo.setPicUrl(category.getPicUrl());
            categoryVo.setKeywords(category.getKeywords());
            categoryVo.setName(category.getName());
            categoryVo.setLevel(category.getLevel());

            ArrayList<CategoryVo> children = new ArrayList<>();
            List<Category> subCategoryList = categoryService.queryByPid(category.getId());
            for (Category subcategory : subCategoryList){
                CategoryVo subCategoryVo = new CategoryVo();
                subCategoryVo.setId(subcategory.getId());
                subCategoryVo.setDesc(subcategory.getDesc());
                subCategoryVo.setIconUrl(subcategory.getIconUrl());
                subCategoryVo.setPicUrl(subcategory.getPicUrl());
                subCategoryVo.setKeywords(subcategory.getKeywords());
                subCategoryVo.setName(subcategory.getName());
                subCategoryVo.setLevel(subcategory.getLevel());

                children.add(subCategoryVo);
            }
            categoryVo.setChildren(children);
            categoryVoList.add(categoryVo);
        }
        return ResponseUtil.okList(categoryVoList);
    }

    @PostMapping("create")
    public Object add(@RequestBody Category category){

        if (category == null){
            return ResponseUtil.errs();
        }
        categoryService.add(category);
        return ResponseUtil.ok();
    }

    @PostMapping("read")
    public Object read(@NotNull Integer id){
        Category category = categoryService.queryById(id);
        return ResponseUtil.ok(category);
    }

    @PostMapping("delete")
    public Object delete(@RequestBody Category category){
        if (category.getId() == null){
            return ResponseUtil.errs();
        }
        categoryService.delete(category.getId());
        return ResponseUtil.ok();
    }

    @PostMapping("update")
    public Object update(@RequestBody Category category){
        if (category == null){
            return ResponseUtil.errs();
        }
        if (categoryService.updateById(category) == 0){
            return ResponseUtil.errs();
        }
    return ResponseUtil.ok();
    }
}

